//
//  AncestorsViewController.m
//  BestPurchase
//
//  Created by ZSMAC on 2018/10/9.
//  Copyright © 2018年 张帅. All rights reserved.
//

#import "AncestorsViewController.h"
#import "MCustomButton.h"

@interface AncestorsViewController ()

@end

@implementation AncestorsViewController
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [MPublicManager sharedPublicManager].currentControllr=self;
    NSString *className = NSStringFromClass([self class]);
 NSLog(@"当前-  %@ will appear",className);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.titleTextAttributes=
  @{NSForegroundColorAttributeName:[UIColor whiteColor],
    NSFontAttributeName:[UIFont systemFontOfSize:18]};
    
    UIImage * image = [UIImage UIImageWithColor:themeColor];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    self.view.backgroundColor=[UIColor whiteColor];
    [self createItemWithTitle:nil selectedImg:@"nav_back_pressed" nomalImg:@"nav_back_pressed" leftEdgeInset:0 isLeft:YES method:@selector(backItemClicked)];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        if (SYSTEM_VERSIONS>=7.0) {
            self.edgesForExtendedLayout = UIRectEdgeNone;
        }
    }
    return self;
}
- (void)backItemClicked{
    [ZSProgressHUD hideAllHUDAnimated:YES];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)View_TouchDown{
    // 发送resignFirstResponder.
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}

- (void)createItemWithTitle:(NSString *)title selectedImg:(NSString *)selectImg nomalImg:(NSString *)nomalImg leftEdgeInset:(CGFloat)edgeInset isLeft:(BOOL)isLeft method:(SEL)method{
    MCustomButton *btn=[MCustomButton buttonWithType:UIButtonTypeCustom];
    UIImage *image=[UIImage imageNamed:nomalImg];
    if (image) {
        if (title) {
            CGSize size=[NSString workOutSizeWithStr:title andFont:BUTTON_FONT value:[NSValue valueWithCGSize:CGSizeMake(999, 40)]];
            [btn setFrame:CGRectMake(0, 0, 30+size.width, 40)];
            btn.imageRect=CGRectMake(0, 5, 30, 30);
            btn.titleRect=CGRectMake(30, 0, size.width, 40);
        }else{
            [btn setFrame:CGRectMake(0, 0, 30, 40)];
            btn.imageRect=CGRectMake(0, 5, 30, 30);
        }
    }else{
        CGSize size=[NSString workOutSizeWithStr:title andFont:BUTTON_FONT value:[NSValue valueWithCGSize:CGSizeMake(999, 40)]];
        [btn setFrame:CGRectMake(0, 0, size.width, 40)];
        //btn.imageRect=CGRectMake(0, 10, 20, 20);
        btn.titleRect=CGRectMake(0, 0, size.width, 40);
    }
    btn.titleLabel.font=[UIFont fontWithName:@"Arial" size:BUTTON_FONT];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:selectImg] forState:UIControlStateHighlighted];
    [btn setImage:[UIImage imageNamed:nomalImg] forState:UIControlStateNormal];
    
    [btn addTarget:self action:method forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleEdgeInsets=UIEdgeInsetsMake(0, edgeInset, 0, 0);
    UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithCustomView:btn];
    if (isLeft) {
        self.navigationItem.leftBarButtonItem=item;
    }else{
        self.navigationItem.rightBarButtonItem=item;
    }
}
- (void)createItemWithTitle:(NSString *)title selectedImg:(NSString *)selectImg nomalImg:(NSString *)nomalImg leftEdgeInset:(CGFloat)edgeInset isLeft:(BOOL)isLeft method:(SEL)method andTitleAttrubutes:(NSDictionary<NSString * ,id>*)attrubutes{
    MCustomButton *btn=[MCustomButton buttonWithType:UIButtonTypeCustom];
    UIImage *image=[UIImage imageNamed:nomalImg];
    if (image) {
        if (title) {
            
            //6236 6842 2000 3683 597
            CGSize size=[NSString workOutSizeWithStr:title andFont:BUTTON_FONT value:[NSValue valueWithCGSize:CGSizeMake(999, 40)]];
            [btn setFrame:CGRectMake(0, 0, 25+size.width, 40)];
            btn.imageRect=CGRectMake(0, 7, 25, 25);
            btn.titleRect=CGRectMake(25, 0, size.width, 40);
        }else{
            [btn setFrame:CGRectMake(0, 0, 25, 40)];
            btn.imageRect=CGRectMake(0, 7, 25, 25);
        }
    }else{
        CGSize size=[NSString workOutSizeWithStr:title andFont:BUTTON_FONT value:[NSValue valueWithCGSize:CGSizeMake(999, 40)]];
        [btn setFrame:CGRectMake(0, 0, size.width, 40)];
        //btn.imageRect=CGRectMake(0, 10, 20, 20);
        btn.titleRect=CGRectMake(0, 0, size.width, 40);
    }
    btn.titleLabel.font=[UIFont fontWithName:@"Arial" size:BUTTON_FONT];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:selectImg] forState:UIControlStateHighlighted];
    [btn setImage:[UIImage imageNamed:nomalImg] forState:UIControlStateNormal];
    
    [btn addTarget:self action:method forControlEvents:UIControlEventTouchUpInside];
    btn.titleEdgeInsets=UIEdgeInsetsMake(0, edgeInset, 0, 0);
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:title attributes:attrubutes];
    [btn setAttributedTitle:att forState:UIControlStateNormal];
    UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithCustomView:btn];
    if (isLeft) {
        self.navigationItem.leftBarButtonItem=item;
    }else{
        self.navigationItem.rightBarButtonItem=item;
    }
}
- (void)NewcreateItemWithTitle:(NSString *)title selectedImg:(NSString *)selectImg nomalImg:(NSString *)nomalImg leftEdgeInset:(CGFloat)edgeInset isLeft:(BOOL)isLeft method:(SEL)method{
    MCustomButton *btn=[MCustomButton buttonWithType:UIButtonTypeCustom];
    UIImage *image=[UIImage imageNamed:nomalImg];
    if (image) {
        if (title) {
            
            //6236 6842 2000 3683 597
            CGSize size=[NSString workOutSizeWithStr:title andFont:BUTTON_FONT value:[NSValue valueWithCGSize:CGSizeMake(999, 40)]];
            [btn setFrame:CGRectMake(0, 0, 25+size.width, 40)];
            btn.imageRect=CGRectMake(0, 7, 25, 25);
            btn.titleRect=CGRectMake(25, 0, size.width, 40);
        }else{
            [btn setFrame:CGRectMake(0, 0, 25, 40)];
            btn.imageRect=CGRectMake(2.5, 10, 20, 20);
        }
    }else{
        CGSize size=[NSString workOutSizeWithStr:title andFont:BUTTON_FONT value:[NSValue valueWithCGSize:CGSizeMake(999, 40)]];
        [btn setFrame:CGRectMake(0, 0, size.width, 40)];
        //btn.imageRect=CGRectMake(0, 10, 20, 20);
        btn.titleRect=CGRectMake(0, 0, size.width, 40);
    }
    btn.titleLabel.font=[UIFont fontWithName:@"Arial" size:BUTTON_FONT];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:selectImg] forState:UIControlStateHighlighted];
    [btn setImage:[UIImage imageNamed:nomalImg] forState:UIControlStateNormal];
    
    [btn addTarget:self action:method forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleEdgeInsets=UIEdgeInsetsMake(0, edgeInset, 0, 0);
    UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithCustomView:btn];
    if (isLeft) {
        UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        spaceItem.width = -5;
        UIBarButtonItem * spaceItem1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        spaceItem1.width = 5;
        self.navigationItem.leftBarButtonItems=@[spaceItem,item,spaceItem1];
    }else{
        self.navigationItem.rightBarButtonItem=item;
    }
}

- (UIButton *)createItemWith:(NSString *)title andNormalImg:(NSString *)normalImg selectImg:(NSString *)selectImg method:(SEL)method{
    MCustomButton *btn=[MCustomButton buttonWithType:UIButtonTypeCustom];
    UIImage *image=[UIImage imageNamed:normalImg];
    if (image) {
        if (title && title.length>0) {
            CGSize size=[NSString workOutSizeWithStr:title andFont:BUTTON_FONT value:[NSValue valueWithCGSize:CGSizeMake(999, 40)]];
            if (size.width<20) {
                size.width=20;
            }
            [btn setFrame:CGRectMake(0, 0, 30+size.width, 40)];
            btn.imageRect=CGRectMake(0, 5, 30, 30);
            btn.titleRect=CGRectMake(30, 0, size.width, 40);
        }else{
            [btn setFrame:CGRectMake(0, 0, 30, 40)];
            btn.imageRect=CGRectMake(0, 5, 30, 30);
        }
    }else{
        CGSize size=[NSString workOutSizeWithStr:title andFont:BUTTON_FONT value:[NSValue valueWithCGSize:CGSizeMake(999, 40)]];
        if (size.width<40) {
            size.width=40;
        }
        [btn setFrame:CGRectMake(0, 0, size.width, 40)];
        btn.titleRect=CGRectMake(0, 0, size.width, 40);
    }
    btn.titleLabel.font=[UIFont fontWithName:@"Arial" size:BUTTON_FONT];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:selectImg] forState:UIControlStateHighlighted];
    [btn setImage:[UIImage imageNamed:normalImg] forState:UIControlStateNormal];
    [btn addTarget:self action:method forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    return btn;
}
- (UIBarButtonItem *)createNavItemWithTitle:(NSString *)title selectedImg:(NSString *)selectImg nomalImg:(NSString *)nomalImg leftEdgeInset:(CGFloat)edgeInset  method:(SEL)method{
    MCustomButton *btn=[MCustomButton buttonWithType:UIButtonTypeCustom];
    UIImage *image=[UIImage imageNamed:nomalImg];
    if (image) {
        if (title) {
            CGSize size=[NSString workOutSizeWithStr:title andFont:BUTTON_FONT value:[NSValue valueWithCGSize:CGSizeMake(999, 40)]];
            [btn setFrame:CGRectMake(0, 0, 30+size.width, 40)];
            btn.imageRect=CGRectMake(0, 5, 30, 30);
            btn.titleRect=CGRectMake(30, 0, size.width, 40);
        }else{
            [btn setFrame:CGRectMake(0, 0, 30, 40)];
            btn.imageRect=CGRectMake(0, 5, 30, 30);
        }
    }else{
        CGSize size=[NSString workOutSizeWithStr:title andFont:BUTTON_FONT value:[NSValue valueWithCGSize:CGSizeMake(999, 40)]];
        [btn setFrame:CGRectMake(0, 0, size.width, 40)];
        //btn.imageRect=CGRectMake(0, 10, 20, 20);
        btn.titleRect=CGRectMake(0, 0, size.width, 40);
    }
    btn.titleLabel.font=[UIFont fontWithName:@"Arial" size:BUTTON_FONT];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:selectImg] forState:UIControlStateHighlighted];
    [btn setImage:[UIImage imageNamed:nomalImg] forState:UIControlStateNormal];
    
    [btn addTarget:self action:method forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn.titleEdgeInsets=UIEdgeInsetsMake(0, edgeInset, 0, 0);
    UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithCustomView:btn];
    return item;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
